package com.example.requestdemo.controller;

import com.example.requestdemo.configs.ExecutorHelper;
import com.example.requestdemo.service.HttpApi;
import com.example.requestdemo.tasks.LoginTask;
import com.example.requestdemo.vo.ResponsesVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.*;

/**
 * @author scxfsc
 */
@RestController
public class RequestController {

    @Resource
    private HttpApi httpApi;

    @Autowired
    private ExecutorHelper executorHelper;

    /**
     * 动态url
     *
     * @param url
     */
    @GetMapping("dynamicUrl/{url}")
    public void dynamic(@PathVariable("url") String url) {
        System.out.println("url --->>> " + url);
        ResponsesVo responsesVo = httpApi.dynamicUrl(url);
        try {
            System.out.println("new ObjectMapper().writeValueAsString(responsesVo) = " + new ObjectMapper().writeValueAsString(responsesVo));
        } catch (JsonProcessingException e) {
            System.out.println("e.getMessage() = " + e.getMessage());
        }
    }

    @GetMapping("getVideoUrl/{timeout}/{formatDate}")
    public List<Map> getVideoUrl(@PathVariable long timeout, @PathVariable String formatDate) {
        // 创建多线程
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
        List<Map> list = new ArrayList<>();
        ScheduledFuture<?> scheduledFuture = executorService.scheduleAtFixedRate(new Runnable() {
            int i = 1;
            int p = 999999;

            @Override
            public void run() {
                ResponsesVo responsesVo = httpApi.queryVideoUrl("" + p);
                System.out.println();
                final int status = 200;
                String msg = "ok";
                if (Objects.equals(responsesVo.getCode(), status) && Objects.equals(responsesVo.getMsg(), msg)) {
                    String videoImgSrc = responsesVo.getData().getVideoimgsrc();
                    if (videoImgSrc.contains(formatDate)) {
                        Map<String, String> map = new LinkedHashMap<>();
                        String videoUrl = responsesVo.getData().getVideourl();
                        System.out.println("pwd = " + p);
                        System.out.println("videoImgSrc = " + videoImgSrc);
                        System.out.println("videoUrl = " + videoUrl);
                        map.put("url", videoUrl);
                        map.put("password", p + "");
                        list.add(map);
                    }
                }
                p = p - this.i;
                if (list.size() >= 2) {
                    executorService.shutdownNow();
                    System.err.println("结束");
                }
                System.out.println();
            }
        }, 0, 1, TimeUnit.MILLISECONDS);

        Object o = null;
        try {
            o = scheduledFuture.get(timeout, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.getMessage();
        } catch (ExecutionException e) {
            e.getMessage();
        } catch (TimeoutException e) {
            e.getMessage();
        } finally {
            System.out.println("o = " + o);
            return list;
        }
    }

    @GetMapping("login")
    public void login() throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorHelper.getDefaultExecutorService();
        int start_num = 929906;
        int end_num = 900000;
        for (int i = start_num; i > end_num; i--) {
            threadPoolExecutor.execute(new LoginTask(httpApi, i + ""));
        }
    }
}
